package com.greenhouse9.prog02.Backup;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;

public class BackupManager {

    private static backup_result br = new backup_result();
    private static HashMap<Integer, backup_result> m_backup = new HashMap<Integer, backup_result> ();
    private static ArrayList<backup_result> m_real = new ArrayList<backup_result>();

	public static void parseBackup(String filename){
        File f1    = new File(filename);

		if (!f1.exists()) {
            System.out.println(f1.getName() + "�͑��݂��܂���B");
			return;
        }

		if(!f1.isFile()) {
			System.out.println(f1.getName() + "�̓t�@�C���ł͂���܂���B");
			return;
		}

		if(!f1.canRead()) {
			System.out.println(f1.getName() + "�͓ǂݍ��߂܂���B");
            return;
		}

		byte [] b_kbn         = new byte[1];
    	byte [] b_backup_no   = new byte[15];
      	byte [] b_backup_name = new byte[30];
      	byte [] b_detail_num  = new byte[20];
      	byte [] b_total_bytes = new byte[20];

        /* �t�@�C���X�g���[���쐬 */
        try {
            InputStream in = new FileInputStream(f1);
            OutputStream out = System.out;
            int ch;
            byte [] b = new byte [87];
            byte [] b2 = new byte [309];
            byte [] b3 = new byte [400];

            m_backup.clear();
            int i2 = 0;
            ArrayList<byte[]> al_b = new ArrayList<byte[]>();
            while((ch = in.read()) != -1){
                 b3 [i2++] = (byte)ch;
                 if ( ch == '\n' ){
                     byte [] bx = new byte [i2];
                     for (int i=0; i < bx.length; i++){
                         bx[i] = b3[i];
                     }
                     al_b.add(bx);
                     i2 = 0;
                 }
            }

            backup_result br = null;

            for (byte [] bx: al_b){

                //System.out.print(new String(bx));

                for (int i=0; i<1; i++){
                    b_kbn [i] = bx[i];
                }

                if (b_kbn[0] == (byte)'1'){

                    System.out.println("���R�[�h�̓w�b�_�ł��B");

                    //�f�[�^�擾
                    String s_backup_no = new String(bx, 1, 15);
                    int    backup_no   = Integer.parseInt(s_backup_no);
                    String backup_name = new String(bx, 16, 30);
                    int    detail_num  = Integer.parseInt(new String(bx, 46, 20));
                    long   total_bytes = Long.parseLong(new String(bx, 66, 20));

                    //�f�[�^�Z�b�g
                    backup_result br2 = new backup_result();

                    br2.setBackupNo(backup_no);
                    br2.setBackName(backup_name);
                    br2.setDetailNum(detail_num);
                    br2.setTotalBytes(total_bytes);
                    br = br2;

                    m_backup.put(br2.getBackupNo(), br2);
                }

                if (b_kbn[0] == (byte)'2'){

                    System.out.println("���R�[�h�͖��ׂł��B");

                    //�f�[�^�擾
                    String s_backup_no = new String(bx, 1, 15);
                    String s_seq       = new String(bx, 16, 10);
                    String s_filename  = new String(bx, 26, 256).trim();
                    String s_file_create_date = new String(bx, 282, 8);
                    String s_file_modified_date = new String(bx, 290, 8);
                    String s_file_size = new String(bx, 298, 10);

                    //�f�[�^�Z�b�g
                    BackupResultDetail brd = new BackupResultDetail();

                    brd.setBackupNo(Integer.parseInt(s_backup_no));
                    brd.setSeq(Integer.parseInt(s_seq));
                    brd.setFileName(s_filename);
                    brd.setFileCreateDate(s_file_create_date);
                    brd.setFileModifiedDate(s_file_modified_date);
                    brd.setFileSize(Integer.parseInt(s_file_size));

                    br.setDetail(brd.getSeq(), brd);

                }

                if (b_kbn[0] == (byte)'9'){
                }

            }

//            while ((ch = in.read(b)) != -1){
//                // b�̓��e�F�m�F
//                out.write(b);
//
//                for (int i=0; i<1; i++){
//                    b_kbn [i] = b[i];
//                }
//
//                if (b_kbn[0] == (byte)'1'){

//                    System.out.println("���R�[�h�̓w�b�_�ł��B");

//                    for (int i=0; i<15; i++){
//                        b_backup_no [i] = b[1 + i];
//                    }

//                    for (int i=0; i<30; i++){
//                        b_backup_name [i] = b[16 + i];
//                    }

//                    for (int i=0; i<20; i++){
//                        b_detail_num [i] = b[46 + i];
//                    }

//                    for (int i=0; i<20; i++){
//                        b_total_bytes [i] = b[66 + i];
//                    }

                    //�f�[�^�擾
//                    String s_backup_no = new String(b_backup_no);
//                    int    backup_no   = Integer.parseInt(s_backup_no);
//                    String backup_name = new String(b_backup_name);
//                    int    detail_num  = Integer.parseInt(new String(b_detail_num));
//                    long   total_bytes = Long.parseLong(new String(b_total_bytes));

                    //�f�[�^�Z�b�g
//                    backup_result br2 = new backup_result();

//                    br2.setBackupNo(backup_no);
//                    br2.setBackName(backup_name);
//                    br2.setDetailNum(detail_num);
//                    br2.setTotalBytes(total_bytes);

  //                  m_backup.put(br2.getBackupNo(), br2);
//                }

//                if (b_kbn[0] == (byte)'2'){

//                    System.out.println("���R�[�h�͖��ׂł��B");

                    //�f�[�^�擾
//                    String s_backup_no = new String(b, 1, 15);
//                    String s_seq       = new String(b, 16, 10);
//                    String s_filename  = new String(b, 26, 256);
//                    String s_file_create_date = new String(b, 282, 8);
//                    String s_file_modified_date = new String(b, 290, 8);
//                    String s_file_size = new String(b, 298, 10);
//                    System.out.println(s_backup_no);
//                    System.out.println(s_seq);
//                    System.out.println(s_filename);
//                    System.out.println(s_file_create_date);
//                    System.out.println(s_file_modified_date);
//                    System.out.println(s_file_size);
                    //int    backup_no   = Integer.parseInt(s_backup_no);
                    //String backup_name = new String(b_backup_name);
                    //int    detail_num  = Integer.parseInt(new String(b_detail_num));
                    //long   total_bytes = Long.parseLong(new String(b_total_bytes));

//                }

//                if (b_kbn[0] == (byte)'9'){
//                }
//            }
            //
            in.close();
            //out.close();

  	    }
  	    catch (IOException e) {
  		    e.printStackTrace();
  	    }


	}

    public static void readReal(String rootdir){
        File f1 = new File(rootdir);

        if (!f1.exists()) {
            System.out.println(f1.getName() + "�͑��݂��܂���B");
            return;
        }

        if(!f1.isDirectory()) {
            System.out.println(f1.getName() + "�̓f�B���N�g���ł͂���܂���B");
            return;
        }

        if(!f1.canRead()) {
            System.out.println(f1.getName() + "�͓ǂݍ��߂܂���B");
            return;
        }

        String [] artist_list = f1.list();
        Arrays.sort(artist_list);

        backup_result br = new backup_result ();
        int seq = 0;

        for ( String a :artist_list){
            System.out.println(a);

            File f_artist = new File(rootdir + "\\" + a);

            if (!f_artist.exists()) {
                System.out.println(f_artist.getName() + "�͑��݂��܂���B");
                return;
            }

            String [] album_list = f_artist.list();

            for (String album: album_list){
                System.out.print("    ");
                System.out.println(album);

                File f_album = new File(f_artist.getAbsolutePath() + File.separator + album);

                String [] music_list = f_album.list();

                for (String music: music_list){
                    System.out.print("        ");
                    System.out.println(music);

                    File f_music = new File(f_album.getAbsolutePath() + File.separator + music);

                    //�f�[�^�Z�b�g
                    BackupResultDetail brd = new BackupResultDetail();

                    //brd.setBackupNo(Integer.parseInt(s_backup_no));
                    seq ++;
                    brd.setSeq(seq);
                    brd.setFileName(f_artist.getName() + File.separator
                                  + f_album.getName() + File.separator + music);
                    //brd.setFileCreateDate(s_file_create_date);
                    //brd.setFileModifiedDate(s_file_modified_date);
                    brd.setFileSize((int)f_music.length());

                    br.setDetail(brd.getSeq(), brd);

                }

            }

        }

        m_real.add(br);

    }

	public static backup_result getBackup () {
		return br;
	}

    public static ArrayList<backup_result> getReal () {
        return m_real;
    }

    public static backup_result getBackup(int i){
        return m_backup.get(i);
    }

}
